package com.snow.useviewdemo.function2.refreshtwo

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.annotation.NonNull
import com.scwang.smart.refresh.header.TwoLevelHeader
import com.scwang.smart.refresh.header.listener.OnTwoLevelListener
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.constant.RefreshState
import com.scwang.smart.refresh.layout.simple.SimpleMultiListener
import com.snow.useviewdemo.R
import com.snow.useviewdemo.base.BaseKotlinActivity


/**
 * Created by zhaoyingtao
 * Date: 2021/10/27
 * Describe:
 */
class SmartRefreshTwoActivity : BaseKotlinActivity() {
    var refreshLayout: SmartRefreshLayout? = null
    var header: TwoLevelHeader? = null
    override fun getLayoutId(): Int {
        return R.layout.activity_smart_refresh_two
    }

    override fun setViewData(savedInstanceState: Bundle?) {
        refreshLayout = findViewById(R.id.refreshLayout)
        header = findViewById(R.id.header)
        refreshLayout?.setOnMultiListener(object : SimpleMultiListener() {
            override fun onLoadMore(@NonNull refreshLayout: RefreshLayout) {
                Toast.makeText(application, "上拉加载", Toast.LENGTH_SHORT).show()
                refreshLayout.finishLoadMore(2000)
            }

            override fun onRefresh(@NonNull refreshLayout: RefreshLayout) {
                Toast.makeText(application, "下拉刷新", Toast.LENGTH_SHORT).show()
                refreshLayout.finishRefresh(2000)
            }

            override fun onStateChanged(refreshLayout: RefreshLayout, oldState: RefreshState, newState: RefreshState) {
                if (oldState == RefreshState.TwoLevel) {
                    findViewById<View>(R.id.second_floor_content).animate().alpha(0f).duration = 1000
                }
            }
        })

        header?.setOnTwoLevelListener(OnTwoLevelListener {
            Toast.makeText(application, "打开二楼", Toast.LENGTH_SHORT).show()
            findViewById<View>(R.id.second_floor_content).animate().alpha(1f).duration = 2000
            true
        })
    }
}